package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.ActiveRankEntity;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.task.ActiveRankTask;
import com.zw.yzk.kabu.data.network.task.VoteTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.view.view.ActiveRankView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class ActiveRankPresenter implements Presenter {

    private UseCase getActiveRank;
    private UseCase vote;
    private AppCompatActivity activity;
    private ActiveRankView view;

    @Inject
    public ActiveRankPresenter(@Named("activeUseCase")UseCase getActiveRank,
                               @Named("voteUseCase")UseCase vote,
                               AppCompatActivity activity) {
        this.getActiveRank = getActiveRank;
        this.vote = vote;
        this.activity = activity;
    }

    public void setView(ActiveRankView view) {
        this.view = view;
    }

    public void getActiveRank(int activeId, int start) {
        getActiveRank.execute(new ActiveRankSubscriber(activity), new ActiveRankTask(activeId, start));
    }

    public void vote(int cosId) {
        vote.execute(new VoteSubscriber(activity), new VoteTask(cosId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getActiveRank.unSubscribe();
        vote.unSubscribe();
    }

    private class VoteSubscriber extends BaseSubscriber<Entity> {

        public VoteSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            view.voteSuccess();
            ToastManager.getInstance().showToast(activity, entity.msg);
        }

        @Override
        public String addErrorMessage() {
            return null;
        }
    }

    private class ActiveRankSubscriber extends BaseSubscriber<ActiveRankEntity> {

        public ActiveRankSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(ActiveRankEntity activeRankEntity) {
            super.onNext(activeRankEntity);
            view.getActiveRankSuccess(activeRankEntity.result.list);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.getActiveRankFailed();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_active_rank_failed);
        }
    }
}
